import instance from './instance'

/**
 * 获取验证码
 * @returns 验证码的src
 */
export function getCode(uuid) {
    let data = instance.get(`/api/user/captcha.jpg`, {
        responseType: 'blob',
        params: {
            uuid
        }
    })
    return new Promise(((resolve) => {
        let reader = new FileReader()
        reader.onload = (e) => resolve(e.target.result)
        data.then(d => reader.readAsDataURL(d))
    }))
}

/**
 *
 * @param loginName 用户名
 * @param password 密码
 * @param captcha 验证码
 * @param uuid 请求验证码时相应的uuid
 * @returns {AxiosPromise<any>}
 */
export function userLogin({loginName, password, captcha, uuid}) {
    return instance.post(`/api/user/login`, {
        loginName, password, captcha, uuid
    })
}

export function userLogout() {
    return instance.post(`/api/user/logout`)
}

/**
 * 获取资源
 * @returns {AxiosPromise<any>}
 */
export function getResource() {
    return instance.get(`/api/sysResource/list`)
}

/**
 * 获取所有角色
 * @returns {AxiosPromise<any>}
 */
export function getRoles() {
    return instance.get(`/api/sysRole/list`)
}

/**
 * 获取所有菜单
 * @returns {AxiosPromise<any>}
 */
export function getMenus() {
    return instance.get(`/api/sysMenu/tree`)
}

export function delMenu(menuId) {
    return instance.delete(`/api/sysMenu/${menuId}`)
}

export function createUpdateMenu(param) {
    return instance.post(`/api/sysMenu/`, {
        ...param
    })
}


export function relateResource(menuId, resId) {
    return instance.post(`/api/sysMenu/${resId}`, {}, {
        params: {
            menuId: menuId
        },
    })
}

export function relateRoleMenu(menuId = [], roleId) {
    return instance.put(`/api/sysRole/menuPerm/${roleId}`, menuId)
}

export function getPublicRoles() {
    return instance.get(`/api/user/roles`)
}

export function register({loginName, password, captcha, uuid, roleId, email, nickName}) {
    return instance.post(`/api/user/register`, {
        loginName, password, captcha, uuid, roleId, email, nickName
    })
}

export function getUserInfo() {
    return instance.get(`/api/user/info`)
}

export function getNav() {
    return instance.get(`/api/sysMenu/nav`)
}

export function createRepairOrder({deviceId, name, type, detail, breakdownTime, productionTime}) {
    return instance.post(`/api/order/create`, {
        deviceId, name, type, detail, breakdownTime: breakdownTime.Format("yyyy-MM-dd")
        , productionTime: productionTime.Format("yyyy-MM-dd")
    })
}


export function getJob(userId) {
    return instance.get(`/api/job/current`, {
        params: {userId}
    })
}

export function completeJob({id, orderId, breakdownInfo, repairInfo}) {
    return instance.post(`/api/job/complete`, {
        id, orderId, breakdownInfo, repairInfo
    })
}


export function postAdvice({id, orderId, repairInfo, cost}) {
    return instance.post(`/api/advice/submit`, {
        id, orderId, repairInfo, cost
    })
}

export function rejectOrder(orderId) {
    return instance.delete(`/api/order/${orderId}`)
}

export function viewAdviceByOrderId(orderId) {
    return instance.get(`/api/advice/user/view`, {
        params: {orderId}
    })
}

export function choiceRepairAdvice({orderId, adviceId}) {
    return instance.post(`/api/advice/choice`, {}, {
        params: {
            orderId, adviceId
        }
    })
}

export function accpetOrder(orderId) {
    return instance.post(`/api/order/accept`, {}, {
        params: {
            orderId
        }
    })
}
